#include <iostream>
#include "opencv2/opencv.hpp"
// #include "/home/xbq/aip-cpp-sdk-4.16.6/face.h"
// #include "opencv2/imgproc.hpp"
// #include "opencv2/objdetect.hpp"

using namespace cv;
using namespace std;
// using namespace aip;

int main()
{
    cv::VideoCapture cap(1);
    if (!cap.isOpened())
    {
        cout << "Camera open failed" << endl;
        return -1;
    }

    cout << "Camera open success!" << endl;
    for (;;)
    {
        Mat img;
        Mat grayImag;
        Mat equalizeImag;
        CascadeClassifier classifier("/usr/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml");
        vector<Rect> allFace;
        Mat faceImage;
        vector<uchar> jpgBuf;
        cap >> img; // get a new frame from camera
        cvtColor(img, grayImag, COLOR_BGR2GRAY);
        equalizeHist(grayImag, equalizeImag);
        classifier.detectMultiScale(equalizeImag, allFace);
        if (allFace.size())
        {
            rectangle(equalizeImag, allFace[0], Scalar(255, 255, 255));
            faceImage = equalizeImag(allFace[0]);
            imencode(".jpg", faceImage, jpgBuf);
        }
        else
        {
            cout << "There is no face!" << endl;
        }

        imshow("video", equalizeImag);
        waitKey(40);
    }

    return 0;
}
